package com.dp.appi.dao.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dp.appi.dao.PackageDao;
import com.dp.appi.exception.PackageRuntimeException;
import com.dp.appi.util.*;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

/**
 * Created by Administrator on 2016/8/25.
 */
@Component
public class PackageDaoImpl extends BaseDaoImpl implements PackageDao {
    /**
     * 获取项目阶段信息
     *
     * @param stage_id
     * @return
     */
    @Override
    public Map<String, Object> projectInfo(Long stage_id) {
        StringBuffer sql = new StringBuffer();
        sql.append(" select ps.project_id,ps.stage_id,ps.uuid as stage_uuid,pb.project_name,pb.project_type,pb.project_attribute,pb.region, ");
        sql.append(" pb.address,pb.detail_address,pb.lng,pb.lat,pb.alt,pb.linkphone,pb.linkman,pb.voice_remark,pb.characters_remark, ");
        sql.append("  pb.weather_zone,pb.power_zone,pb.wire_type,pb.codeset_version_id,ps.stage,ps.manager manager_id,ps.explorationer explorationer_id, ");
        sql.append(" ps.designer designer_id,ps.auditor auditor_id,ps.name as stage_name,ps.design_unit design_unit_id, ");
        sql.append(" ps.owner_unit owner_unit_id,sd1.departname design_unit,sd2.departname owner_unit ,CONCAT(tst.typename,ccv.codeset_version) codeset_version");
        sql.append(" from p_stage ps ");
        sql.append(" LEFT JOIN p_baseinfo pb on ps.project_id=pb.project_id ");
        sql.append(" LEFT JOIN t_s_depart sd1 on ps.design_unit = sd1.ID ");
        sql.append(" LEFT JOIN t_s_depart sd2 on ps.owner_unit = sd2.ID ");
        sql.append(" LEFT JOIN c_codeset_version ccv  on pb.codeset_version_id = ccv.codeset_version_id");
        sql.append(" LEFT JOIN t_s_type tst on tst.typecode = ccv.codeset_type and tst.typegroupid = 'sds807081511112c501521x1ebc2rr04'");
        sql.append(" where ps.stage_id=? and ps.deleted=0 ");

        Map projectInfo = findOneObject(sql.toString(), new Object[]{stage_id});

        if (projectInfo == null) {
            projectInfo = new HashMap<>();
        }

        //人员转义 manager explorationer designer auditor
        projectInfo.put("manager", escapeUser(projectInfo.get("manager_id"), ","));
        projectInfo.put("explorationer", escapeUser(projectInfo.get("explorationer_id"), ","));
        projectInfo.put("designer", escapeUser(projectInfo.get("designer_id"), ","));
        projectInfo.put("auditor", escapeUser(projectInfo.get("auditor_id"), ","));

        //土壤信息
        String sql2 = "select pso.soil_type,pso.soil_value from p_stage ps" +
                " LEFT JOIN p_soil pso on pso.project_id=ps.project_id where ps.stage_id=?";
        List<Map<String, Object>> list2 = getJdbcTemplate().queryForList(sql2, new Object[]{stage_id});
        projectInfo.put("soil", list2);
        //二次转运
        String sql3 = "select pst.transport_type,pst.distance from p_stage ps" +
                " LEFT JOIN p_second_transport pst on pst.project_id=ps.project_id where ps.stage_id=?";
        List<Map<String, Object>> list3 = getJdbcTemplate().queryForList(sql3, new Object[]{stage_id});
        projectInfo.put("transport", list3);
        return projectInfo;
    }


    /**
     * 阶段踏勘点集合
     *
     * @param stage_id
     * @return
     */
    @Override
    public List<Map<String, Object>> explorationList(Long stage_id, Integer exploration_type, Integer classify) {
        StringBuffer sb = new StringBuffer();
        sb.append(" select pe.exploration_id,pe.uuid,pe.number,pe.exploration_type,pe.codeset_id,pe.before_exp,pe2.uuid as before_uuid,pe.pt_id,ppt.pt_type,pe.pt_objectid,");
        sb.append(" pe.lng,pe.lat,pe.alt,pe.distance,pe.corner,ps.current_schedule as schedule_id,pe.location_way,aup.user_name,");
        sb.append(" pe.explor_id,pe2.lng as before_lng,pe2.lat as before_lat");
        sb.append(" from p_exploration pe");
        sb.append(" LEFT JOIN p_pt ppt on pe.pt_id=ppt.pt_id");
        sb.append(" LEFT JOIN p_stage ps on pe.stage_id=ps.stage_id");
        sb.append("  LEFT JOIN a_user_point aup on (aup.id=pe.pt_objectid and pe.pt_id=100005)");
        sb.append(" LEFT JOIN p_exploration pe2 ON (pe.before_exp=pe2.exploration_id and pe.before_exp!=0 and pe2.deleted=0)");
        sb.append(" where pe.deleted=0 and ps.deleted=0");
        sb.append(" and pe.stage_id=?");
        if (exploration_type != null) {
            sb.append(" and pe.exploration_type = ?");//新建/拆旧
        }
        sb.append("  and pe.classify=?");//固定，获取一类数据
//        sb.append(" and ppt.pt_type = 11");//固定，只取定位点
        sb.append(" order by pe.exploration_id desc");
        if (exploration_type != null)
            return findForJdbc(sb.toString(), new Object[]{stage_id, exploration_type, classify});
        else
            return findForJdbc(sb.toString(), new Object[]{stage_id, classify});
    }


    /**
     * 获取台区信息
     *
     * @param stage_id
     * @param exploration_type
     * @param classify
     * @return
     */
    @Override
    public List<Map<String, Object>> transformerList(Long stage_id, Integer exploration_type, Integer classify) {
        StringBuilder sb = new StringBuilder();
        sb.append(" select pex.exploration_id,pex.uuid,pex.exploration_type,pex.number,atr.`name` from p_exploration pex");
        sb.append("  LEFT JOIN a_transformer atr on pex.pt_objectid = atr.id");
        sb.append("  where pex.pt_id = '100002'");
        sb.append(" and pex.deleted = 0");
        sb.append(" and pex.stage_id = ?");
        if (exploration_type != null)
            sb.append(" and pex.exploration_type = " + exploration_type);

        sb.append(" and pex.classify =? ");

        return findForJdbc(sb.toString(), new Object[]{stage_id, classify});
    }


    /**
     * 踏勘点点位信息
     *
     * @param exploration_id
     * @return
     */
    @Override
    public Map<String, Object> explorInfo(Long exploration_id) {
        String sql = "select pe.exploration_id,pe.uuid,pe.number,pe1.exploration_id before_exp,pe1.number before_number,pe.distance,pe.exploration_type,pe.pt_id,pe.pt_objectid,pe.lng,pe.voltage_grade," +
                "pe.lat,pe.alt,pe.codeset_id,cc.number codeset_number,cc.`name` codeset_name,pe.corner,pe.location_way  " +
                " from p_exploration pe" +
                " LEFT JOIN c_codeset cc" +
                " on pe.codeset_id = cc.codeset_id" +
                " LEFT JOIN p_exploration pe1" +
                " on pe.before_exp = pe1.exploration_id and pe1.deleted=0" +
                " where pe.exploration_id=? and pe.deleted=0";
        Map<String, Object> map = findOneObject(sql, new Object[]{exploration_id});

        if (map != null && null != map.get("pt_objectid")) {
            map.put("pt", this.pointInfo(Long.parseLong(String.valueOf(map.get("pt_objectid"))), Long.parseLong(String.valueOf(map.get("pt_id")))));
        }
        return map;
    }


    /**
     * 踏勘点设计信息
     *
     * @param id
     * @param pt_id
     * @return
     */
    @Override
    public Map<String, Object> pointInfo(Long id, Long pt_id) {
        String sql = "select * from p_pt where pt_id=? and is_deleted=0 and is_valid=1";
        Map<String, Object> map = queryForMap(sql, pt_id);
        if (map != null) {
            String sql2 = "select * from " + map.get("pt_table") + " where id=?";
            Map<String, Object> map2 = queryForMap(sql2, id);
            return map2;
        }
        return null;
    }


    /**
     * 获取点位的上联信息
     *
     * @param exploration_id
     * @return
     */
    @Override
    public Map<String, Object> beforeExp(Long exploration_id) {
        StringBuilder sb = new StringBuilder();
        sb.append(" select pex1.exploration_id,pex1.uuid,pex1.before_exp,pex2.number as before_num,pex1.distance from p_exploration pex1");
        sb.append(" INNER JOIN p_exploration pex2 on pex1.before_exp = pex2.exploration_id");
        sb.append("  where pex2.deleted = 0");
        sb.append(" and pex1.exploration_id = ?");

        return findOneForJdbc(sb.toString(), new Object[]{exploration_id});
    }


    /**
     * 踏勘点与上联点位中间的跨越物信息列表
     *
     * @param exploration_id
     * @return
     */
    @Override
    public List<Map<String, Object>> channelAList(Long exploration_id) {
        StringBuffer sb = new StringBuffer();
        sb.append(" select pc.channel_id,pc.uuid,fea.pt_id,fea.pt_objectid,");
        sb.append(" fea.distance,fea.exploration_id,fea.uuid as exploration_uuid,fea.number from p_channel pc");
        sb.append(" INNER JOIN p_exploration fea ON fea.exploration_id=pc.feature_id");
        sb.append(" LEFT JOIN p_pt pt on fea.pt_id=pt.pt_id");
        sb.append(" where pc.exploration_id=? and pt.pt_type = 12  and pt.is_deleted = 0 and fea.deleted = 0 ORDER BY fea.distance asc");

        return getJdbcTemplate().queryForList(sb.toString(), new Object[]{exploration_id});
    }


    /**
     * 踏勘点环境描述信息
     *
     * @param exploration_id
     * @return
     */
    @Override
    public Map<String, Object> envInfo(Long exploration_id) {
        StringBuffer sql = new StringBuffer();
        sql.append("select environment_id,uuid,traffic, ");
        sql.append("width,haul_distance,voice_remark,characters_remark ");
        sql.append("from p_environment ");
        sql.append("where exploration_id = ?");
        return queryForMap(sql.toString(), exploration_id);
    }

    /**
     * 获取点位表名
     *
     * @param ptId
     * @return
     */
    @Override
    public String getPointTableName(Long ptId) {
        String sql = "select pt_table from p_pt where pt_id=? and is_deleted=0";
        Map<String, Object> map = findOneForJdbc(sql, new Object[]{ptId});
        if (map != null) {
            return String.valueOf(map.get("pt_table"));
        } else {
            throw new PackageRuntimeException(SYS.PACK0004.name());
        }
    }


    @Override
    public String projectPack(String rootPath, Map<String, String> param, Integer classify) throws IOException {
        boolean flag;
        //项目阶段id
        String stageId = param.get("stageId");

        String packDirName = FileHandel.packDir + stageId;//项目文件夹名称
        String base_dir_path = rootPath + packDirName;//项目总文件夹路径
        //     G://ProjectStage_4147

        //删除路径下原来文件及压缩包
        flag = FileHandel.deleteDirAndZip(base_dir_path);

        if (flag) {
            /**
             * 系统文件夹
             */
            File base_dir = new File(base_dir_path);
            base_dir.mkdir();

            /**
             * 项目阶段信息
             */
            //文件创建
            String projectStageBaseInfo_fileName = FileHandel.projectStageBaseInfo + stageId + FileHandel.fileEnd;//项目阶段信息文件名称
            String projectStageBaseInfo_filePath = base_dir_path + File.separator + projectStageBaseInfo_fileName;
            File projectStageBaseInfo_file = new File(projectStageBaseInfo_filePath);
            projectStageBaseInfo_file.createNewFile();
            //获取项目阶段信息并写入相应文本
            Map<String, Object> projectInfo = this.projectInfo(Long.valueOf(stageId));
            FileHandel.autoWritter(projectStageBaseInfo_file, projectInfo);

            /**
             * 踏勘点位列表
             */

            List<Map<String, Object>> list_11 = this.explorationList(Long.valueOf(stageId), 11, classify);
            if (list_11.size() > 0) {
                //文件创建
                String projectStageExplorePointList_fileName = FileHandel.projectStageExplorePointList + stageId + FileHandel.projectStageExplorePointListNnd_11 + FileHandel.fileEnd;
                String projectStageExplorePointList_filePath = base_dir_path + File.separator + projectStageExplorePointList_fileName;
                File projectStageExplorePointList_file = new File(projectStageExplorePointList_filePath);
                Map map_explorationList_11 = new HashMap();
                map_explorationList_11.put("explorationList", list_11);

                //文件写入
                FileHandel.autoWritter(projectStageExplorePointList_file, map_explorationList_11);
            }

            List<Map<String, Object>> list_12 = this.explorationList(Long.valueOf(stageId), 12, classify);
            if (list_12.size() > 0) {
                //文件创建
                String projectStageExplorePointList_fileName = FileHandel.projectStageExplorePointList + stageId + FileHandel.projectStageExplorePointListNnd_12 + FileHandel.fileEnd;
                String projectStageExplorePointList_fielPath = base_dir_path + File.separator + projectStageExplorePointList_fileName;
                File projectStageExplorePointList_fiel = new File(projectStageExplorePointList_fielPath);

                //文件写入
                Map map_explorationList_12 = new HashMap();
                map_explorationList_12.put("explorationList", list_12);
                FileHandel.autoWritter(projectStageExplorePointList_fiel, map_explorationList_12);
            }

            /**
             * 台区信息列表
             */
            List<Map<String, Object>> list_trf_11 = this.transformerList(Long.valueOf(stageId), null, classify);
            if (list_trf_11.size() > 0) {
                //文件创建
//                String projectTransformerList_fileName = FileHandel.projectTransformerList + stageId + FileHandel.projectStageExplorePointListNnd_11 + FileHandel.fileEnd;
                String projectTransformerList_fileName = FileHandel.projectTransformerList + stageId + FileHandel.fileEnd;
                String projectTransformerList_filePath = base_dir_path + File.separator + projectTransformerList_fileName;
                File projectTransformerList_file = new File(projectTransformerList_filePath);

                Map map_projectTransformerList = new HashMap();
                map_projectTransformerList.put("explorationList", list_trf_11);
                //文件写入
                FileHandel.autoWritter(projectTransformerList_file, map_projectTransformerList);
            }

//            List<Map<String, Object>> list_trf_12 = this.transformerList(Long.valueOf(stageId), 12, classify);
//            if (list_trf_12.size() > 0) {
//                //文件创建
//                String projectTransformerList_fileName = FileHandel.projectTransformerList + stageId + FileHandel.projectStageExplorePointListNnd_12 + FileHandel.fileEnd;
//                String projectTransformerList_filePath = base_dir_path + File.separator + projectTransformerList_fileName;
//                File projectTransformerList_file = new File(projectTransformerList_filePath);
//
//                //文件写入
//                FileHandel.autoWritter(projectTransformerList_file, list_trf_12);
//            }

            /**
             * 踏勘点信息
             */
            List<Map<String, Object>> listAll = this.explorationList(Long.valueOf(stageId), null, classify);

            for (int i = 0; i < listAll.size(); i++) {
                String exploreId = listAll.get(i).get("exploration_id").toString();

                //上联信息
                Map<String, Object> map_befor = this.beforeExp(Long.valueOf(exploreId));
                if (map_befor != null) {
                    String projectExploreAcrossBeforePoint_fileName = FileHandel.projectExploreAcrossBeforePoint + exploreId + FileHandel.fileEnd;
                    String projectExploreAcrossBeforePoint_filePath = base_dir_path + File.separator + projectExploreAcrossBeforePoint_fileName;
                    File projectExploreAcrossBeforePoint_file = new File(projectExploreAcrossBeforePoint_filePath);

                    Map projectExploreAcrossBeforePoint = new HashMap();
                    projectExploreAcrossBeforePoint.put("afterExp", map_befor);
                    FileHandel.autoWritter(projectExploreAcrossBeforePoint_file, projectExploreAcrossBeforePoint);
                }

                //基本信息+设计信息
                Map<String, Object> map_explor = this.explorInfo(Long.valueOf(exploreId));
                String projectExplorePointInfo_fileName = FileHandel.projectExplorePointInfo + exploreId + FileHandel.fileEnd;
                String projectExplorePointInfo_filePath = base_dir_path + File.separator + projectExplorePointInfo_fileName;
                File projectExplorePointInfo_file = new File(projectExplorePointInfo_filePath);
                FileHandel.autoWritter(projectExplorePointInfo_file, map_explor);

                //踏勘点与上联点位中间的跨越物信息
                List<Map<String, Object>> list_ky = this.channelAList(Long.valueOf(exploreId));
                if (list_ky.size() > 0) {
                    String projectExploreAcrossOnChannelList_fileName = FileHandel.projectExploreAcrossOnChannelList + exploreId + FileHandel.fileEnd;
                    String projectExploreAcrossOnChannelList_filePath = base_dir_path + File.separator + projectExploreAcrossOnChannelList_fileName;
                    File projectExploreAcrossOnChannelList_file = new File(projectExploreAcrossOnChannelList_filePath);

                    Map projectExploreAcrossOnChannelList = new HashMap();
                    projectExploreAcrossOnChannelList.put("channelAList", list_ky);

                    FileHandel.autoWritter(projectExploreAcrossOnChannelList_file, projectExploreAcrossOnChannelList);
                }


                //踏勘点环境描述信息
                Map<String, Object> map_ev = this.envInfo(Long.valueOf(exploreId));
                if (map_ev != null) {
                    String grojectExploreEnvInfo_fileName = FileHandel.grojectExploreEnvInfo + exploreId + FileHandel.fileEnd;
                    String grojectExploreEnvInfo_filePath = base_dir_path + File.separator + grojectExploreEnvInfo_fileName;
                    File grojectExploreEnvInfo_file = new File(grojectExploreEnvInfo_filePath);

                    Map grojectExploreEnvInfo = new HashMap();
                    grojectExploreEnvInfo.put("envInfo", map_ev);


                    FileHandel.autoWritter(grojectExploreEnvInfo_file, grojectExploreEnvInfo);
                }

                //项目照片信息ProjectExplorePointInfoPhoto_
                Map<String, Object> map_photo = this.expPhotoList(Long.valueOf(exploreId));
                String projectExploreEnvInfo_fileName = FileHandel.projectExplorePointInfoPhoto + exploreId + FileHandel.fileEnd;
                String projectExploreEnvInfo_filePath = base_dir_path + File.separator + projectExploreEnvInfo_fileName;
                File projectExploreEnvInfo_file = new File(projectExploreEnvInfo_filePath);
                FileHandel.autoWritter(projectExploreEnvInfo_file, map_photo);
            }

        }

//        数据打包
//        ZipHandle zh = new ZipHandle(base_dir_path + ".zip");
//        int i = zh.compressor(base_dir_path);
//        File file = new File(base_dir_path+".zip");
//        if (i == 1) {
//            return packDirName + ".zip";
//        }
//        else
//            return "";
        return FileHandel2.zip(base_dir_path, base_dir_path + ".zip");
    }


    /**
     * 重新计算当前点位的夹角
     *
     * @param id
     */
    @Override
    public void calculateConer(Long id) {
        //当前点位信息
        String pex_sql = "select * from p_exploration where exploration_id = ?";
        Map<String, Object> pex_map = findOneObject(pex_sql, new Object[]{id});
        Double point_lng = Double.valueOf(pex_map.get("lng").toString());
        Double point_lat = Double.valueOf(pex_map.get("lat").toString());

        //当前点位的上联id
        Object point_befor = pex_map.get("before_exp");

        String sql_update_corner = "update p_exploration set corner=? where exploration_id =?";
        Double corner = null;

        if (point_befor == null) {//上联为空,当前点位角度为null
            executeSql(sql_update_corner, new Object[]{null, id});//
        } else {
            //上联点位信息
            Long point_befor_id = Long.valueOf(point_befor.toString());
            String sql_up = "select * from p_exploration px WHERE px.exploration_id = ?";
            Map<String, Object> map = findOneForJdbc(sql_up, new Object[]{point_befor_id});
            Double up_lng = Double.valueOf(map.get("lng").toString());
            Double up_lat = Double.valueOf(map.get("lat").toString());

            //点位下联对象集合
            String sql_down = "SELECT t1.* from p_exploration as t1 inner join p_pt as t2 on t1.pt_id=t2.pt_id " +
                    "where t1.before_exp = ? and t2.pt_type=11";
            List<Map<String, Object>> List_down = findForJdbc(sql_down, new Object[]{id});
            if (List_down.isEmpty()) {//下联为空，当前点角度为null
                executeSql(sql_update_corner, new Object[]{null, id});
            } else {
                if (List_down.size() == 1) {//下联点位数量为1，重新计算当前点位角度
                    Double down_lng = Double.valueOf(List_down.get(0).get("lng").toString());
                    Double down_lat = Double.valueOf(List_down.get(0).get("lat").toString());

                    double distance_point_up = PointUtil.GetLongDistance(point_lng, point_lat, up_lng, up_lat);
                    double distance_point_down = PointUtil.GetLongDistance(point_lng, point_lat, down_lng, down_lat);
                    double distance_up_down = PointUtil.GetLongDistance(up_lng, up_lat, down_lng, down_lat);

                    double a = Math.pow(distance_point_up, 2) + Math.pow(distance_point_down, 2) - Math.pow(distance_up_down, 2);
                    double b = 2 * distance_point_up * distance_point_down;

                    double k = a / b;
                    if (k > 1)
                        k = 1;
                    else if (k < -1)
                        k = -1;

                    corner = Math.acos(k) * 180 / Math.PI;
//                        corner = Math.acos((Math.pow(distance_point_up, 2) + Math.pow(distance_point_down, 2) - Math.pow(distance_up_down, 2)) / (2 * distance_point_up * distance_point_down))*180/Math.PI;
                    if (corner > 90) {
                        corner = 180 - corner;
                    }
                    if (!corner.isNaN()) {
                        executeSql(sql_update_corner, new Object[]{corner, id});
                    }
                } else {//下联点位大于1，当前点位角度为null
                    executeSql(sql_update_corner, new Object[]{null, id});
                }

            }
        }
    }

    @Override
    public Map<String, Object> expPhotoList(Long exploration_id) {
        //一类照片
        String sql1 = "select photo_id,exploration_id,photo_type,photo_thumb,photo_url,uuid from p_exploration_photo where photo_type=? and exploration_id=? ";
        List<Map<String, Object>> list1 = getJdbcTemplate().queryForList(sql1, new Object[]{1, exploration_id});
        //二类照片
        List<Map<String, Object>> list2 = getJdbcTemplate().queryForList(sql1, new Object[]{2, exploration_id});
        //三类照片
        List<Map<String, Object>> list3 = getJdbcTemplate().queryForList(sql1, new Object[]{3, exploration_id});
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> result1 = new HashMap<>();
        result1.put("photoNum", list1.size());
        result1.put("photoList", list1);
        Map<String, Object> result2 = new HashMap<>();
        result2.put("photoNum", list2.size());
        result2.put("photoList", list2);
        Map<String, Object> result3 = new HashMap<>();
        result3.put("photoNum", list3.size());
        result3.put("photoList", list3);
        result.put("onePhoto", result1);
        result.put("twoPhoto", result2);
        result.put("threePhoto", result3);
        return result;
    }

    @Override
    public void up_explor_byBYQID(Long exploration_id,Long explor_id) {
        //更新台区标识
        String up_explor = "update p_exploration set explor_id = ? where exploration_id = ? ";

        //查询下联所有点位
        String sel_down_exp = "select exploration_id from p_exploration where deleted=0 and exploration_type=11 and before_exp = ?";
        this.getJdbcTemplate().update(up_explor, new Object[]{explor_id, exploration_id});
        List<Map<String, Object>> list = findForJdbc(sel_down_exp, new Object[]{exploration_id});//下联点位集合
        for (int i = 0; i < list.size(); i++) {
            exploration_id = Long.valueOf(String.valueOf(list.get(i).get("exploration_id")));
            this.getJdbcTemplate().update(up_explor, new Object[]{explor_id,exploration_id});
//            list = findForJdbc(sel_down_exp, new Object[]{exploration_id});
            up_explor_byBYQID(exploration_id,explor_id);
        }

    }

    /**
     * 获取用户的业主单位和设计单位
     *
     * @param id 用户id
     * @return
     */
    @Override
    public Map<String, Object> getUnit(String id) {
        String sql="select tsuo.user_id,tsuo.org_id,tsd.org_type from t_s_user_org tsuo " +
                " INNER JOIN t_s_depart tsd on tsd.ID = tsuo.org_id " +
                " where tsuo.user_id=?";
        return findOneObject(sql,new Object[]{id});
    }


}
